<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2019/12/5
 * Time: 14:51
 */

namespace app\api\controller;


use think\Db;
use think\exception\DbException;

class My extends Base
{
    private $user_id;
    private $page;
    private $create_time;
    
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        $this->user_id = input('user_id');
        $this->page = input('page') ? input('page') : 1;
        $this->create_time = date('Y-m-d H:i:s');
        $time = input('time');
        $value = input('value');
        $reqKey = input('reqKey');
        $auth = $this->checkAll($this->user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }
    
    /**
     * 用户基本信息
     * @return \think\response\Json
     */
    public function userInfo(){
        $info = Db::table('up_u_user')
            ->join('up_area ua1', 'ua1.district_id = user_sheng', 'left')
            ->join('up_area ua2', 'ua2.district_id = user_shi', 'left')
            ->join('up_area ua3', 'ua3.district_id = user_qu', 'left')
            ->where('user_id', $this->user_id)
            ->field('user_username, user_image, user_gender, ua1.district province, ua2.district city, 
            ua3.district, user_role, user_sheng, user_shi, user_qu')
            ->find();
        if($info){
            return json(['status' => 200, 'msg' => $info]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 编辑个人信息
     * @return \think\response\Json
     * user_image     头像
     * user_username  昵称
     * user_gender    性别  1 男  2 女
     * user_sheng     省ID
     * user_shi       市ID
     * user_qu        区ID
     */
    public function updateUser(){
        $param = input('');
        $update['user_update_time'] = $this->create_time;
        $update['user_image'] = $param['user_image'];
        $update['user_username'] = $param['user_username'];
        $update['user_gender'] = $param['user_gender'];
        $update['user_sheng'] = $param['user_sheng'];
        $update['user_shi'] = $param['user_shi'];
        $update['user_qu'] = $param['user_qu'];
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '编辑成功']);
        }else{
            return json(['status' => 0, 'msg' => '编辑失败']);
        }
    }
    
    /**
     * 推送开关
     * @return \think\response\Json
     */
    public function showSend(){
        $is_send = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_is_send');
        return json(['status' => 200, 'msg' => $is_send]);
    }
    
    /**
     * 打开/关闭消息推送开关
     * @return \think\response\Json
     */
    public function doSend(){
        $is_send = Db::table('up_u_user')->where('user_id', $this->user_id)->value('user_is_send');
        if($is_send == 1){
            $user_is_send = 2;
        }else{
            $user_is_send = 1;
        }
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->setField('user_is_send', $user_is_send);
        if($res){
            return json(['status' => 200, 'msg' => '操作成功']);
        }else{
            return json(['status' => 0, 'msg' => '操作失败']);
        }
    }
    
    /**
     * 我的收藏-商品
     * @return \think\response\Json
     */
    public function myCollect(){
        $collect = Db::table('up_collect')
            ->join('up_goods', 'goods_id = collect_goods_id', 'left')
            ->field('collect_id, goods_id, goods_name, goods_thumb, goods_price')
            ->where('collect_user_id', $this->user_id)
            ->page($this->page, 10)
            ->order('collect_time desc')
            ->select();
        if($collect){
            return json(['status' => 200, 'msg' => $collect]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 删除收藏
     * @return \think\response\Json
     * collect_id    收藏ID
     */
    public function delCollect(){
        $collect_id = input('collect_id');
        $res = Db::table('up_collect')->delete($collect_id);
        if($res){
            return json(['status' => 200, 'msg' => '删除成功']);
        }else{
            return json(['status' => 0, 'msg' => '删除失败']);
        }
    }
    
    /**
     * 修改密码
     * @return \think\response\Json
     * old_pwd    原密码
     * f_pwd      新密码
     * s_pwd      确认新密码
     */
    public function changePassword(){
        $old_pwd = input('old_pwd');
        $f_pwd = input('f_pwd');
        $s_pwd = input('s_pwd');
        
        $user_pwd = Db::table('up_u_user')
            ->where('user_id', $this->user_id)
            ->value('user_password');
        
        if(md5($old_pwd) != $user_pwd){
            return json(['status' => 0, 'msg' => '原密码输入错误']);
        }
        if($f_pwd != $s_pwd){
            return json(['status' => 0, 'msg' => '两次新密码输入的不一样']);
        }
        $update = [
            'user_password' => md5($f_pwd),
            'user_update_time' => $this->create_time
        ];
        $res = Db::table('up_u_user')->where('user_id', $this->user_id)->update($update);
        if($res){
            return json(['status' => 200, 'msg' => '修改成功']);
        }else{
            return json(['status' => 0, 'msg' => '修改失败']);
        }
    }
    
    /**
     * 上传头像
     * @return \think\response\Json
     * file    图像
     */
    public function uploadImage(){
        $file = request()->file('file');
        $dir = ROOT_PATH . 'public' . DS . 'uploads/images';
        if(!file_exists($dir)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($dir, 0700,true);
        }
        $url =  http_type ();
        $info = $file->move($dir);
        if($info){
            $newName = $info->getSaveName();
            $image_url = $url . "/uploads/images/{$newName}";
            return json(['status' => 200, 'msg' => $image_url]);
        }else{
            return json(['status' => 0, 'msg' => '上传错误']);
        }
    }
    
    /**
     * 用于判断用户是否开通车辆网
     * @return \think\response\Json
     */
    public function clw(){
        // 用户车辆网信息
        $clw = Db::table('up_u_user')
            ->field('user_car_id, user_team_id')
            ->where('user_id', $this->user_id)
            ->find();
        // 车辆网申请信息
        $clw_info = Db::table('up_vehicle')
            ->field('vehicle_status')
            ->where('vehicle_user_id', $this->user_id)
            ->find();
        if($clw['user_team_id']){
            $have = 2;
        }else{
            if($clw_info){
                if($clw_info['vehicle_status'] == 1){
                    $have = 1; // 已提交，待审核
                }elseif($clw_info['vehicle_status'] == 2){
                    $have = 2; // 已提交，审核通过
                }else{
                    $have = 3; // 已提交，审核拒绝
                }
            }else{
                $have = 4; // 未提交
            }
        }
        return json(['status' => 200, 'msg' => $clw, 'have' => $have]);
    }
}